package com.platform.service.impl;

import com.platform.dao.RoleDao;
import com.platform.domain.TbSysRole;
import com.platform.domain.result.TreeGridData;
import com.platform.domain.result.TbSysRoleFunction;
import com.platform.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-8-31
 * Time: 下午1:34
 * To change this template use File | Settings | File Templates.
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao;

    /**
     * 根据用户ID查询用户拥有的权限
     *
     * @param userId 用户ID
     * @return 权限集合
     * @throws Exception
     */
    @Override
    public List<TbSysRole> queryRoleByUserId(String userId) throws Exception {
        return roleDao.queryRoleByUserId(userId);
    }

    /**
     * 根据用户ID查询符合条件的角色权限DataGridTree模型
     *
     * @param userId 用户ID
     * @return DataGridTree模型
     * @throws Exception
     */
    @Override
    @Deprecated
    public List<TreeGridData> queryRoleFunctionByUserId(String userId) throws Exception {
        // 根据用户ID查询出对象角色权限集合
        List<TbSysRoleFunction> list = roleDao.queryRoleFunctionByUserId(userId);
        // 进行显示DataGridTree模型转换
        List<TreeGridData> dgTree = new ArrayList<TreeGridData>();
        if (list != null && list.size() > 0) {
            for(TbSysRoleFunction sf : list) {
                TreeGridData dgTreeData = null;
                String roleId = sf.getRole().getRoleId();
                for(TreeGridData temp : dgTree) {
                    if (roleId.equals(temp.getId())) {
                        dgTreeData = temp;
                        break;
                    }
                }
                if (dgTreeData == null) {
                    dgTreeData = new TreeGridData();
                    dgTreeData.setId(roleId);
                    dgTreeData.setText(sf.getRole().getRoleName());
                    TreeGridData dgTreeDataFunction = new TreeGridData();
                    dgTreeDataFunction.setId(sf.getFunction().getFunctionId());
                    dgTreeDataFunction.setText(sf.getFunction().getFunctionName());
                    dgTreeDataFunction.setState("open");
                    dgTreeData.setChildren(new ArrayList<TreeGridData>());
                    dgTreeData.getChildren().add(dgTreeDataFunction);
                    dgTree.add(dgTreeData);
                } else {
                    String funSuperior = sf.getFunction().getFunSuperior();
                    if (funSuperior != null && funSuperior.trim().length() > 0) {
                        for (TreeGridData dgtData : dgTreeData.getChildren()) {
                            if (funSuperior.equals(dgtData.getId())) {
                                if (dgtData.getChildren() == null) {
                                    dgtData.setChildren(new ArrayList<TreeGridData>());
                                }
                                TreeGridData dgTreeDataFunction = new TreeGridData();
                                dgTreeDataFunction.setId(sf.getFunction().getFunctionId());
                                dgTreeDataFunction.setText(sf.getFunction().getFunctionName());
                                dgtData.getChildren().add(dgTreeDataFunction);
                                break;
                            }
                        }

                    } else {
                        TreeGridData dgTreeDataFunction = new TreeGridData();
                        dgTreeDataFunction.setId(sf.getFunction().getFunctionId());
                        dgTreeDataFunction.setText(sf.getFunction().getFunctionName());
                        dgTreeData.getChildren().add(dgTreeDataFunction);
                    }
                }
            }
        } else {
            TreeGridData dgtd = new TreeGridData();
            dgtd.setId("ROLE");
            dgtd.setText("没有符合条件的记录！");
            dgTree.add(dgtd);
        }
        return dgTree;
    }

    /**
     * 根据用户ID查询除用户授权角色以外的角色集合
     *
     * @param userId 用户ID
     * @return 角色集合
     * @throws Exception
     */
    @Override
    public List<TbSysRole> queryRoleByNotUserId(String userId) throws Exception {
        return roleDao.queryRoleByNotUserId(userId);
    }
}
